/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.biz.Page;
import net.biz.Pageable;
import net.biz.entity.Brand;
import net.biz.entity.Brand.Type;
import net.biz.entity.Product;
import net.biz.service.BrandService;
import net.biz.service.OrderService;
import net.biz.service.ProductService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.home766.dto.OrderBrandCountDTO;
import com.home766.entity.TeHuiManager;
import com.home766.service.TeHuiManagerService;

/**
 * Controller - 品牌
 * 
 * @author BIZ Team
 * @version 3.0
 */
@Controller("shopBrandController")
@RequestMapping("/brand")
public class BrandController extends BaseController {

	@Resource(name = "brandServiceImpl")
	private BrandService brandService;
	@Resource(name="orderServiceImpl")
	private OrderService orderService;
	@Resource(name="productServiceImpl")
	private ProductService productService;
	@Resource(name="teHuiManagerServiceImpl")
	private TeHuiManagerService teHuiManagerService;
	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(ModelMap model,Pageable pageable,Boolean bool) {
		Page<Brand> page = new Page<Brand>();
		List<Brand> list = brandService.findAll();
		if(bool!=null&&bool){
			List<Brand> newList=new ArrayList<Brand>();
			if(list!=null){
				for(Brand b:list){
					if(b.getType()==Type.image){
						newList.add(b);
					}
				}
			}
			page=new Page<>(newList, newList.size(), pageable);
		}
		//统计每个品牌单品的已购买数量
		List<OrderBrandCountDTO> countLists = orderService.countOrderBrandNumber();
		for(Brand brand :page.getContent()){
			Long id = brand.getId();
			for(OrderBrandCountDTO dto : countLists){
				if(dto.getBrand() == id){
					brand.setCountNumber(dto.getCountNumber());
				}
			}
		}
		List<TeHuiManager> teHuiManager = teHuiManagerService.findAll();
		model.addAttribute("teHuiManager",teHuiManager);
		model.addAttribute("page", page);
		return "/shop/brand/list";
	}

	/**
	 * 内容
	 */
	@RequestMapping(value = "/content", method = RequestMethod.GET)
	public String content(Long id, ModelMap model,Pageable pageable) {
		Brand brand = brandService.find(id);
		if (brand == null) {
			return ERROR_VIEW;
		}
		Page<Product> page = productService.findPage(null, brand, null, null, null, null, null, true, true, null, false, null, null, null, pageable);
		model.addAttribute("page", page);
		model.addAttribute("brand", brand);
		model.addAttribute("id", id);
		
		return "/shop/brand/content";
	}
	/**
	 * 内容
	 */
	@RequestMapping(value = "/content/{id}", method = RequestMethod.GET)
	public String contentById(@PathVariable Long id, ModelMap model,Pageable pageable) {
		Brand brand = brandService.find(id);
		if (brand == null) {
			return ERROR_VIEW;
		}
		Page<Product> page = productService.findPage(null, brand, null, null, null, null, null, true, true, null, false, null, null, null, pageable);
		model.addAttribute("page", page);
		model.addAttribute("brand", brand);
		model.addAttribute("id", id);
		
		return "/shop/brand/content";
	}

}